package com.lyh.hkcloud.module.common;

import android.app.Activity;
import android.util.Log;
import android.widget.EditText;
import com.lyh.hkcloud.constants.UrlConstants;
import com.lyh.hkcloud.util.base.JsonUtil;
import com.lyh.hkcloud.util.base.LoadingUtils;
import com.lyh.hkcloud.util.base.StringUtils;
import com.lyh.hkcloud.util.base.ToastUtils;
import com.lyh.hkcloud.util.http.Request;
import com.lyh.hkcloud.util.http.listener.json.JsonFailuredListener;
import com.lyh.hkcloud.util.http.listener.json.JsonSuccessListener;
import org.json.JSONObject;

public class WmsModuleService extends ModuleService {


    public WmsModuleService(Activity activity) {
        super(activity);
    }

    public void getWhids(final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        Request.post(activity, UrlConstants.BUS_ROOT + "/wms9060/getUserWhid", JsonUtil.getBlankJSONObject(), new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                if (!"0000".equals(response.optString("code"))) {
                    ToastUtils.makeText(activity, response.optString("msg"), ToastUtils.LENGTH_SHORT).show();
                    failuredListener.onRespone(response.optString("msg"), null);
                } else {
                    successListener.onRespone(response.optString("msg"), response);
                }
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                ToastUtils.makeText(activity, msg, ToastUtils.LENGTH_SHORT).show();
                failuredListener.onRespone(msg, response);
            }
        });
    }

    public void checkBox(final String boxId,final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        if(StringUtils.isEmpty(boxId)){
            failuredListener.onRespone(null, null);
            return;
        }
        LoadingUtils.getInstance(activity).show();
        JSONObject jsonObject = JsonUtil.put("boxId", boxId);
        Request.post(activity, UrlConstants.BUS_ROOT + "/wms9060/getBox", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                if (!"0000".equals(response.optString("code"))) {
                    ToastUtils.makeText(activity, response.optString("msg"), ToastUtils.LENGTH_SHORT).show();
                    failuredListener.onRespone(response.optString("msg"), null);
                }else {
                    successListener.onRespone(response.optString("msg"), JsonUtil.getJSONObject(response,"data"));
                }
                LoadingUtils.getInstance(activity).hide();
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                LoadingUtils.getInstance(activity).hide();
                ToastUtils.makeText(activity, msg, ToastUtils.LENGTH_SHORT).show();
                failuredListener.onRespone(msg, response);
            }
        });
    }

    public void checkLocation(final EditText et,final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        if (et == null || StringUtils.isEmpty(et.getText().toString().trim())) {
            failuredListener.onRespone(null, null);
            return;
        }
        LoadingUtils.getInstance(activity).show();
        JSONObject jsonObject = JsonUtil.put("locationId", et.getText().toString().trim());
        Request.post(activity, UrlConstants.BUS_ROOT + "/wms9060/getLocation", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                if (!"0000".equals(response.optString("code"))) {
                    ToastUtils.makeText(activity, response.optString("msg"), ToastUtils.LENGTH_SHORT).show();
                    failuredListener.onRespone(response.optString("msg"), null);
                }else {
                    successListener.onRespone(response.optString("msg"), JsonUtil.getJSONObject(response,"data"));
                }
                LoadingUtils.getInstance(activity).hide();
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                LoadingUtils.getInstance(activity).hide();
                ToastUtils.makeText(activity, msg, ToastUtils.LENGTH_SHORT).show();
                failuredListener.onRespone(response.optString("msg"), null);
            }
        });
    }
}
